package latex;

import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;


public class LatexTableOptionsPanel extends JPanel {
	private static final long serialVersionUID = -6592699802830432108L;
	private JTextField textFormatValues;
	private JTextField textMultiplikator;
	private JTextField textSpaltenueberschrift;
	private JComboBox cmbBoxLinien;
	
	public static LatexTableFormat format=null;
	private JTextField txtColumns;


	/**
	 * Create the frame.
	 */
	public LatexTableOptionsPanel(JButton btnOk, JButton btnCancel) {
//		setAlwaysOnTop(true);
		setVisible(true);
//		setResizable(false);
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0,0, 500, 357);
		setSize(500, 357);
		setBorder(new EmptyBorder(5, 5, 5, 5));
//		setContentPane(contentPane);
		setLayout(null);
		
		
		
		JLabel lblTabelle = new JLabel("LATEX-Tabelle");
		lblTabelle.setBounds(5, 5, 421, 35);
		lblTabelle.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblTabelle.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(lblTabelle);
		
		textFormatValues = new JTextField();
		textFormatValues.setBounds(5, 111, 421, 20);
		this.add(textFormatValues);
		textFormatValues.setColumns(10);
		
		JLabel lblFormat = new JLabel("Spaltenformat");
		lblFormat.setBounds(5, 91, 137, 20);
		this.add(lblFormat);
		
		JLabel lblSpaltenmultiplikator = new JLabel("Spaltenmultiplikatoren *10^x");
		lblSpaltenmultiplikator.setBounds(5, 142, 170, 14);
		this.add(lblSpaltenmultiplikator);
		
		textMultiplikator = new JTextField();
		textMultiplikator.setBounds(5, 157, 421, 20);
		this.add(textMultiplikator);
		textMultiplikator.setColumns(10);
		
		JLabel lblSpaltenberschrift = new JLabel("Spalten\u00FCberschriften");
		lblSpaltenberschrift.setBounds(5, 180, 137, 14);
		this.add(lblSpaltenberschrift);
		
		textSpaltenueberschrift = new JTextField();
		textSpaltenueberschrift.setBounds(5, 195, 421, 20);
		this.add(textSpaltenueberschrift);
		textSpaltenueberschrift.setColumns(10);
		
		cmbBoxLinien = new JComboBox(LatexTableLines.values());
		cmbBoxLinien.setBounds(5, 239, 209, 20);
		this.add(cmbBoxLinien);
		
		JLabel lblLinien = new JLabel("Linien");
		lblLinien.setBounds(5, 222, 137, 20);
		this.add(lblLinien);
		
		
		btnOk.setBounds(50, 282, 154, 36);
		this.add(btnOk);
		System.out.println(btnOk.getActionCommand());
		
		btnCancel.setBounds(220,282,154,36);
		this.add(btnCancel);
		
		txtColumns = new JTextField();
		txtColumns.setBounds(5, 70, 421, 20);
		add(txtColumns);
		txtColumns.setColumns(10);
		
		JLabel lblSpalten = new JLabel("Spalten");
		lblSpalten.setBounds(5, 55, 46, 14);
		add(lblSpalten);
		validate();
		repaint();
	}
	
	public void initializeTextFields(int columnNumber) {
		String formatText="";
		String columnsText="";
		for(int i=0; i<columnNumber; i++){
			formatText+="0.00 ";
			columnsText+=i+" ";
		}
		textFormatValues.setText(formatText.trim());
		txtColumns.setText(columnsText.trim());
		
		String multiplicator="";
		for(int i=0; i<columnNumber; i++){
			multiplicator+="0 ";
		}
		textMultiplikator.setText(multiplicator.trim());
		String ueberschriften="";
		for(int i=0; i<columnNumber; i++){
			ueberschriften+="col"+i+" ";
		}
		textSpaltenueberschrift.setText(ueberschriften.trim());
	}

//	public static void requestFormat(int columnNumber, KorrelationMittelungLatex korrelationMittelung){
//		korrelationMittelungLatex=korrelationMittelung;
//		LatexTablePrompt frame=new LatexTablePrompt(columnNumber);
////		while(format==null){
////			try {
////				Thread.sleep(30);
////			} catch (InterruptedException e) {
////				e.printStackTrace();
////			}
////		}
//	}

	public LatexTableFormat getFormat() {
		ArrayList<Integer> multiplier=new ArrayList<Integer>();
		String[] multsplit=textMultiplikator.getText().trim().split(" ");
		for(int i=0; i<multsplit.length; i++){
			multiplier.add(Integer.parseInt(multsplit[i]));
		}
		ArrayList<String> colCap=new ArrayList<String>();
		String[] colCapSplit=textSpaltenueberschrift.getText().trim().split(" ");
		for(int i=0; i<colCapSplit.length; i++){
			colCap.add(colCapSplit[i]);
		}
		ArrayList<String> formatStrings=new ArrayList<String>();
		String[] formatSplit=textFormatValues.getText().trim().split(" ");
		for(int i=0; i<formatSplit.length; i++){
			formatStrings.add(formatSplit[i]);
		}
		LatexTableLines lines=(LatexTableLines)(cmbBoxLinien.getSelectedItem());
		ArrayList<Integer> columns=new ArrayList<Integer>();
		String[] colSplit=txtColumns.getText().split(" ");
		for(int i=0; i<colSplit.length; i++){
			columns.add(Integer.parseInt(colSplit[i]));
		}
		
		
		setVisible(false);
		format=new LatexTableFormat(multiplier, colCap, formatStrings, lines, columns);
		return format;
	}
}
